//
//  BN_SuggestionBoxViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-15.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_SuggestionBoxViewController.h"
#import "BN_SuggestBox.h"

@interface BN_SuggestionBoxViewController () {
    BN_SuggestBox *suggestBoxData;
    MJRefreshHeaderView *_header;
}

@end

@implementation BN_SuggestionBoxViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [suggestBoxData.helper cancelQuerySuggestInfo];
    [suggestBoxData.helper cancelQuerySuggestList];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [[NSUserDefaults standardUserDefaults] setInteger:SUGGESTIONBOX forKey:USERDEFAULTS_KINDERSECTION];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    // UI
    self.tabBarController.tabBar.hidden = YES;
    
    // UI+64
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"意见箱";
    
    [self schoolNavgationBar:4];

    // 列表
    self.suggestBoxTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT) style:UITableViewStylePlain];
    [_suggestBoxTableView setDecelerationRate:0.1];

    if (!suggestBoxData) {
        suggestBoxData = [[BN_SuggestBox alloc]init];
    }
    __weak typeof(self) weakSelf = self;
    [suggestBoxData toSchoolReloadList:^(BOOL isReload,BOOL isDele,NSArray *indexArr) {
        if (!isDele) {
            if (isReload) {
                [weakSelf cancelHintView];
                [weakSelf.suggestBoxTableView reloadData];
            }else {
                [weakSelf cancelHintView];
            }
        }else {
            [LZCHINT title:@"意见删除成功" type:LZCSuccess duration:3 places:HINT_PLACES callBack:NULL];
            [weakSelf.suggestBoxTableView reloadData];
        }

    }];
    
    switch (MAIN_VERSION) {
        case 6:
            self.suggestBoxTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            break;
        default:
            self.suggestBoxTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            self.suggestBoxTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            self.suggestBoxTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
            break;
    }
    self.suggestBoxTableView.delegate = suggestBoxData;
    self.suggestBoxTableView.dataSource = suggestBoxData;
    // 清楚多余边框
    [self setExtraCellLineHidden:self.suggestBoxTableView];
    _suggestBoxTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    // TableView
    if ([self.suggestBoxTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.suggestBoxTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    [self.view addSubview:self.suggestBoxTableView];

    // 下拉刷新
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.suggestBoxTableView;
    header.delegate = suggestBoxData;
    _header = header;
    
    // 小球提示
    [self viewLoadHintView];
    
    // 通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(openXQPage:) name:KKINDER_OPEN_BOX_XQ object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suggestModelRefresh:) name:KSENDSUCCESS_AFTER_SUGGEST_REFRESH object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(boxDele:) name:K_SCHOOL_BOX_DELE object:nil];

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVC)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

#pragma mark 
#pragma mark Action

- (void)boxDele:(NSNotification *)notification {
    [suggestBoxData boxModelRemove:notification.object];
}

- (void)suggestModelRefresh:(NSNotification *)notification {
    [suggestBoxData boxRefrsh];
}

- (void)backRootVC {
    [self releaseResouce];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goCreateVC {
    BN_CreateContentViewController *createVC = [[BN_CreateContentViewController alloc]init];
    [createVC toHintMessage:^(BOOL isSuccess) {
        if (isSuccess) {
            [LZCHINT title:@"更新成功" type:LZCSuccess duration:2 places:HINT_PLACES callBack:NULL];
        }
    }];
    [self.navigationController pushViewController:createVC animated:YES];
}

- (void)openXQPage:(NSNotification *)notification {
    NSString *boxId = notification.object;
    BN_ContentXQViewController *kindexqVC_ = [[BN_ContentXQViewController alloc]init];
    kindexqVC_.objctId = boxId;
    [self.navigationController pushViewController:kindexqVC_ animated:YES];
}

#pragma mark
#pragma mark 生命周期

- (void)releaseResouce {
    
    [_header free];
    _header.delegate = nil;
    _header = nil;
    
    self.suggestBoxTableView.delegate = nil;
    self.suggestBoxTableView.dataSource = nil;
    self.suggestBoxTableView = nil;
    
    suggestBoxData.reloadList = nil;
    [suggestBoxData.helper cancelQuerySuggestList];
    suggestBoxData.helper = nil;
    suggestBoxData = nil;
    
    [self cancelHintView];
}

- (void)dealloc
{
    [self releaseResouce];
    NSLog(@"意见箱释放...");
}

@end
